const app = getApp();

Page({
  data: {
    password: '',
    confirmPassword: '',
  },

  // 更新密码输入
  handleInputChange(e) {
    const { field } = e.currentTarget.dataset;
    const value = e.detail;

    this.setData({
      [field]: value
    });
  },
  onPullDownRefresh(){
    this.onLoad()
},
  // 提交修改密码请求
  updatePassword() {
    const { password, confirmPassword } = this.data;

    if (!password || !confirmPassword) {
      wx.showToast({ title: '请填写密码', icon: 'error' });
      return;
    }

    if (password !== confirmPassword) {
      wx.showToast({ title: '两次密码输入不一致', icon: 'error' });
      return;
    }

    const token = wx.getStorageSync('token');
    wx.request({
      url: `${app.apiUrl}/users/update/password`, // 确保 userId 已存储
      method: 'PUT',
      header: {
        'Content-Type': 'application/json',
        token: token
      },
      data: { password },
      success: res => {
        if (res.data.code === 1) {
          wx.showToast({ title: '密码修改成功', icon: 'success' });
          wx.navigateBack();
        } else {
          wx.showToast({ title: res.data.msg || '修改失败', icon: 'error' });
        }
      },
      fail: () => {
        wx.showToast({ title: '网络错误，请稍后重试', icon: 'error' });
      }
    });
  }
});
